//
//  JSLBillHomeController.m
//  JingleSpeedLoan
//
//  Created by 宋志彬 on 2018/8/2.
//  Copyright © 2018年 宋质彬. All rights reserved.
//

#import "JSLBillHomeController.h"
#import "JSLBillHomeCell.h"
#import "JSLWarnRepmentController.h"
#import "JSLBillJiBiController.h"
@interface JSLBillHomeController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger totalPage;
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@property (nonatomic , strong) UIImageView *headerView;
@property (nonatomic , weak) UILabel *moneyLab;
@end

@implementation JSLBillHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self baseConfig:PageGobackTypeNone];
    [self tableView];
    
    self.tableView.tableHeaderView = self.headerView;
    
    self.arrDataSource = [NSMutableArray arrayWithArray:@[@{ @"name":@"叮当账单",@"image" : @"bill-bijic"}, @{@"name":@"账单提醒",@"image" : @"bill-warn"}
                                                         
                                                          ]];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.navigationController.navigationBar.prefersLargeTitles = NO;
        
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    
    
    if ([UserManager isLogin]) {
        NSMutableArray *arr =  [CacheManager getObject:@"LOAN_BILL_DATST"];
        
        CGFloat totleMoney = 0.00;
        for (NSDictionary *info in arr) {
            NSInteger timestamp_now = [self getNowTimestamp];
            
            NSInteger timestamp_some = [self timeSwitchTimestamp:info[@"repaymentTime"] andFormatter:@"YYYY-MM-dd"];
            if (timestamp_now<timestamp_some) {
                CGFloat lixiValue = [info[@"moneylixi"] floatValue]*0.01 *[info[@"loanTime"] integerValue]*[info[@"money"] floatValue];
                
                CGFloat totleValue = lixiValue +[info[@"money"] floatValue];
                
                totleMoney = totleMoney +totleValue;
                
            }
        }
        
        
        self.moneyLab.text = [NSString stringWithFormat:@"%.2f",totleMoney];
    }else {
        self.moneyLab.text = [NSString stringWithFormat:@"0.00"];
    }
    
    
}

-(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值
    return timeSp;
    
}
#pragma mark - 获取当前时间的 时间戳

-(NSInteger)getNowTimestamp{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间
    
    NSLog(@"设备当前的时间:%@",[formatter stringFromDate:datenow]);
    
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
    NSLog(@"设备当前的时间戳:%ld",(long)timeSp); //时间戳的值
    
    
    
    return timeSp;
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
#pragma mark - 请求数据
- (void)refreshData {
    
//    NSArray * arr = [CacheManager getObject:@"SYJ_TRADE_LIST"];
//    self.arrDataSource = [NSMutableArray arrayWithArray:arr];
//    [self.tableView reloadData];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
    });
   
    
}

- (void)getMoreData {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_footer endRefreshing];
    });
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrDataSource.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JSLBillHomeCell *cell = [JSLBillHomeCell cellWithTableView:tableView];
    cell.info = self.arrDataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        if ([UserManager checkLogin:self]) {
        JSLBillJiBiController *vc = [[JSLBillJiBiController alloc] init];
        [self pushViewController:vc animated:YES];
            
        }
    }else if (indexPath.row ==1) {
        
        if ([UserManager checkLogin:self]) {
            JSLWarnRepmentController *vc = [[JSLWarnRepmentController alloc] init];
            [self pushViewController:vc animated:YES];
        }
        
        
    }
    
}

#pragma mark - get
- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-64 -49 ) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.scrollEnabled = YES;
        _tableView.rowHeight = 100 *AutoSizeScaleYY +20*AutoSizeScaleYY;
        _tableView.backgroundColor = BACKGROUND_COLOR;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_tableView];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
        
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
        
//        _tableView.tableHeaderView = [JSLKitFactory jsl_viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10*AutoSizeScaleYY) backgroundColor:BACKGROUND_COLOR superView:nil];
        
    }
    
    return _tableView;
}

- (UIView *)headerView {
    if (_headerView == nil) {
        _headerView = [JSLKitFactory jsl_imageViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180*AutoSizeScaleYY) imageName:@"bill-home-back" superView:nil];
        
        CGSize billSize = [@"账单" sizeWithAttributes:@{NSFontAttributeName : MEDIUM_FONT(17)}];
        UILabel *titleLab = [JSLKitFactory jsl_labelWithFrame:CGRectMake(0, 34, SCREEN_WIDTH, billSize.height) text:@"叮当账单" font:MEDIUM_FONT(17) textColor:[UIColor whiteColor] textAlignment:NSTextAlignmentCenter SuperView:_headerView];
        
        CGSize warnSize = [@"近30日应还(元)" sizeWithAttributes:@{NSFontAttributeName : MEDIUM_FONT(12)}];
        UILabel *warnTitleLab = [JSLKitFactory jsl_labelWithFrame:CGRectMake(0, titleLab.bottom +20*AutoSizeScaleYY, SCREEN_WIDTH, warnSize.height) text:@"账单待还金额(元)" font:MEDIUM_FONT(12) textColor:[UIColor whiteColor] textAlignment:NSTextAlignmentCenter SuperView:_headerView];
        
        CGSize moneySize = [@"3000" sizeWithAttributes:@{NSFontAttributeName : MEDIUM_FONT(30)}];
        self.moneyLab = [JSLKitFactory jsl_labelWithFrame:CGRectMake(0, warnTitleLab.bottom +15*AutoSizeScaleYY, SCREEN_WIDTH, moneySize.height) text:@"3000" font:MEDIUM_FONT(30) textColor:[UIColor whiteColor] textAlignment:NSTextAlignmentCenter SuperView:_headerView];
        

    }
    return _headerView;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
